package top.yazif.text;

import org.apache.commons.collections4.map.LinkedMap;
import top.yazif.excel.ExcelImpl;
import top.yazif.exception.ExcelException;
import top.yazif.exception.ExcelIndexOutOfBoundsException;

import java.io.File;
import java.util.*;

/**
 * Created by Yazif on 2017/3/24.
 */
public class ExcelImplCountTest {
    public static void main(String[] args) throws Exception {
        File dir = new File("e:/temp/temp/");

        Map<String, Integer>  map = new HashMap<>();
        Map<String, List<String>> valueMap = new HashMap<>();
        File[] filelist = dir.listFiles();
        for(File file : filelist){
            System.out.println("now deal with :" + file.getName());
            ExcelImpl excel = new ExcelImpl(file.getAbsolutePath());
            int rowCount = excel.getNumberOfRows();
            for(int rowIndex = 2;rowIndex < rowCount; rowIndex += 2){
                int colCount = excel.getNumberOfCell(rowIndex);
                for(int colIndex = 1; colIndex < colCount; colIndex ++){
                    String value = excel.getValue(rowIndex, colIndex);
                    if(!"".equals(value)){
                        String mapKey = String.valueOf(rowIndex) + ":" + String.valueOf(colIndex);
                        Integer count = map.get(mapKey);
                        if(count == null){
                            count = 1;
                            map.put(mapKey,count);
                            List<String> course = new ArrayList<>();
                            course.add(file.getName().split("\\.")[0] + ":" + value);
                            valueMap.put(mapKey, course);
                        }else {
                            count++;
                            map.put(mapKey, count);
                            valueMap.get(mapKey).add(file.getName().split("\\.")[0] + ":" + value);
                        }
                    }
                }
            }
            excel.close();
        }

        ExcelImpl excelWrite = new ExcelImpl("e:/temp/1.xlsx");
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while ( it.hasNext()){
            Map.Entry<String,  Integer> en = it.next();
            String[] keys = en.getKey().split("\\:");
            int rowIndex = Integer.valueOf(keys[0]);
            int colIndex = Integer.valueOf(keys[1]);
            excelWrite.setCellValue(String.valueOf(en.getValue().intValue()),rowIndex, colIndex);
        }

        Iterator<Map.Entry<String, List<String>>> it2 = valueMap.entrySet().iterator();
        while ( it2.hasNext()){
            Map.Entry<String,  List<String>> en = it2.next();
            String[] keys = en.getKey().split("\\:");
            int rowIndex = Integer.valueOf(keys[0]);
            int colIndex = Integer.valueOf(keys[1]);


        }


        excelWrite.close();

    }
}
